package com.example.droidwarehouse.item;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import android.util.Log;

import com.example.droidwarehouse.async.Communicator;
import com.example.droidwarehouse.config.Config;

/**
 * Class that communicates with backend service for communication
 * 
 * @author Bibek Shrestha <bibek.shrestha@tum.de>
 * @author David Schattel(extending Javadoc)
 * @since 15.11.2012
 */
public class ItemService
{
    private final String urlRoot = (String) Config.get("service.url"); // Server
	                                                               // URL
    private final String urlStatus = urlRoot + "status.php";
    private final String urlRegister = urlRoot + "register.php";
    private final String urlRent = urlRoot + "rent.php";
    private final String urlRetrieve = urlRoot + "retrieve.php";
    private final String urlItemName = urlRoot + "itemname.php";

    private final String TAG = "ItemService";

    // different states of a warehouse item
    public static enum ItemStatus
    {
	UNKNOWN, AVAILABLE, RENTED
    }

    /**
     * Get the current status of a specific warehouse item
     * 
     * @param item
     *            Warehouse item object
     * @return Status of the warehouse item
     */
    public ItemStatus getStatus(final Item item)
    {
	Map<String, String> params = new HashMap<String, String>();
	params.put("id", item.getId());

	String response = communicatePost(urlStatus, params).trim();
	String itemName = communicatePost(urlItemName, params).trim();
	Log.d(TAG, "getStatus: " + response);

	// forwarding the servers answer about the item status
	if (response.equals("unknown"))
	{
	    return ItemStatus.UNKNOWN;
	}
	else if (response.equals("available"))
	{
	    item.setName(itemName);
	    return ItemStatus.AVAILABLE;
	}
	else if (response.equals("rented"))
	{
	    item.setName(itemName);
	    return ItemStatus.RENTED;
	}
	else
	{
	    return ItemStatus.UNKNOWN;
	}
    }

    /**
     * Registering a new item / adding it to the database
     * 
     * @param item
     *            New warehouse item
     * @return Answer from server, if adding item to database was successful or
     *         not
     */
    public boolean register(final Item item)
    {
	Map<String, String> params = new HashMap<String, String>();
	params.put("id", item.getId());
	params.put("name", item.getName());

	String response = communicatePost(urlRegister, params);
	if (response == "success")
	{
	    return true;
	}
	else
	{
	    return false;
	}
    }

    /**
     * Renting a new item / changing its status and adding the name of the
     * person who rented it
     * 
     * @param item
     *            Warehouse item which is rented
     * @param person
     *            Person's name who rents the item
     * @return Answer from server, if renting item was successful or not
     */
    public boolean rent(final Item item, final Person person)
    {
	Map<String, String> params = new HashMap<String, String>();
	params.put("id", item.getId());
	params.put("cust_name", person.getName());

	String response = communicatePost(urlRent, params);
	if (response == "success")
	{
	    return true;
	}
	else
	{
	    return false;
	}
    }

    /**
     * Retreive a warehouse item after a customer gave it back / setting status
     * to available
     * 
     * @param item
     *            Warehouse item
     * @return Answer from server, if retrieving item was successful or not
     */
    public boolean retreive(Item item)
    {
	Map<String, String> params = new HashMap<String, String>();
	params.put("id", item.getId());

	String response = communicatePost(urlRetrieve, params);
	if (response == "success")
	{
	    return true;
	}
	else
	{
	    return false;
	}
    }

    /**
     * Communication with the server
     * 
     * @param targetURL
     *            URL of the php webservice
     * @param params
     *            parameters about what to send and what is its value
     * @return result of the Communicator object
     */
    private String communicatePost(String targetURL, Map<String, String> params)
    {
	String result = null;
	try
	{
	    result = new Communicator().execute(new Object[] { targetURL, params }).get();
	}
	catch (InterruptedException e)
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	catch (ExecutionException e)
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	return result;
    }
}
